package cp3120.view;

import cp3120.dao.DAOFactory;
import cp3120.model.Flight;
import generic.swing.tabs.Tab;
import java.awt.BorderLayout;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 *
 * @author Adam Scarr <scarr.adam@gmail.com>
 */
public class MainFrame extends JFrame {
    private MainMenu menu;
    private FlightList flightList;
    private JTabbedPane tabbedPane = new JTabbedPane();
    private JMenuBar loginMenu = new JMenuBar();
    private JPanel loginPanel = new JPanel(new BorderLayout());

    public MainFrame() {
        super("Airsoft v1337");

        menu = new MainMenu();
        add(menu, BorderLayout.NORTH);

        flightList = new FlightList(DAOFactory.getFlightDAO().getFlights());
        tabbedPane.addTab("All Flights", flightList);
        //tabbedPane.setTabComponentAt(0, new Tab("All Flights", tabbedPane));

        add(tabbedPane, BorderLayout.CENTER);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();

        loginMenu.add(new JMenu("File"));

        loginPanel.setMinimumSize(flightList.getMinimumSize());
        loginPanel.setPreferredSize(flightList.getPreferredSize());
        loginPanel.setMaximumSize(flightList.getMaximumSize());
        loginPanel.add(new JLabel("Please wait, logging in...", JLabel.CENTER), BorderLayout.CENTER);

    }

    public FlightList getFlightList() {
        return flightList;
    }

    public void showFlightlist(String name, List<Flight> flights) {
        FlightList flightList = new FlightList(flights);
        tabbedPane.addTab("All Flights", flightList);
        tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(flightList), new Tab(name, tabbedPane));
        tabbedPane.setSelectedComponent(flightList);
    }

    public void setLoggingIn(boolean loggingIn) {
        if (loggingIn) {
            remove(menu);
            remove(tabbedPane);
            add(loginMenu, BorderLayout.NORTH);
            add(loginPanel, BorderLayout.CENTER);
        } else {
            remove(loginMenu);
            remove(loginPanel);
            add(menu, BorderLayout.NORTH);
            add(tabbedPane, BorderLayout.CENTER);
        }
        pack();
        repaint();
    }

    public MainMenu getMenu() {
        return menu;
    }

    public void update() {
        menu.update();
        flightList.update(DAOFactory.getFlightDAO().getFlights());
        repaint();
    }
}
